package cn.tedu.boyou.comment.service.impl;

import cn.tedu.boyou.comment.domain.VirtualzcComment;
import cn.tedu.boyou.comment.repository.IVirtualzcCommentRepository;
import cn.tedu.boyou.comment.service.IVirtualzcCommentService;
import cn.tedu.boyou.common.query.VirtualzcCommentQuery;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Date;
import java.util.List;

@Service
public class VirtualzcCommentServiceImpl implements IVirtualzcCommentService {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private IVirtualzcCommentRepository dao;

    public Page<VirtualzcComment> query(VirtualzcCommentQuery qo) {
        Query query = new Query();

        long total = mongoTemplate.count(query, VirtualzcComment.class);
        if(total == 0){
            return Page.empty();
        }
        Pageable pageable = PageRequest.of(qo.getCurrentPage() -1, qo.getPageSize());
        query.with(pageable);
        Sort createTime = Sort.by(Sort.Direction.DESC, "createTime");
        query.with(createTime);
        List<VirtualzcComment> list = mongoTemplate.find(query, VirtualzcComment.class);

        return new PageImpl<VirtualzcComment>(list,pageable,total);
    }

    public void save(VirtualzcComment virtualzcComment) {
        virtualzcComment.setCreateTime(new Date());
        dao.save(virtualzcComment);
    }
}
